
<?php

$db =  require dirname(dirname(dirname(__FILE__)))."/data/conf/db.php";

$db_name = $db["DB_NAME"];
define("DB_NAME",$db_name);
$db_host = $db["DB_HOST"];
define("DB_HOST",$db_host);
$db_user = $db["DB_USER"];
define("DB_USER",$db_user);
$DB_PWD = $db["DB_PWD"];
define("DB_PASSWD",$DB_PWD);
define("db",DB_NAME);
class MyConnect{
    function link_mysql(){

        $con = mysql_connect(DB_HOST,DB_USER,DB_PASSWD);
        //$con = mysql_connect('192.100.1.30','web','S#per*2!or3');
        mysql_query("set names 'utf8'");
        if (!$con)
        {
            die('Could not connect: ' . mysql_error());  //结束语句

        }

        return $con;
    }
    //查询数据
    //查询数据
    function selectData($table,$parm = array()){
        $con = myconnect::link_mysql();
        mysql_select_db(db,$con);	//连接数据库
        $sql = "SELECT * FROM ".$table;
        if(!empty($parm)){
            $a = 0;
            foreach($parm as $pindex => $pitem){
                if($a == 0){
                    $sql = $sql." WHERE ".$pindex." = '".$pitem."' ";
                }else{
                    $sql = $sql." AND ".$pindex." = '".$pitem."' ";
                }
                $a = 1;
            }
        }

        $result = mysql_query($sql,$con);	//	查询数据库表
        $arr = array();
        if(empty($result)){
            return false;
        }

        while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
            $arr[] = $row;
        }
        unset($row);
        mysql_close($con);
        return empty($arr)?'':$arr[0];
    }
    //查询数据
    function selectDataArr($table,$parm = array(),$order = ""){
        $con = myconnect::link_mysql();
        mysql_select_db(db,$con);	//连接数据库
        $sql = "SELECT * FROM ".$table;
        if(!empty($parm)){
            $a = 0;
            foreach($parm as $pindex => $pitem){
                if($a == 0){
                    $sql = $sql." WHERE ".$pindex." = '".$pitem."' ";
                }else{
                    $sql = $sql." AND ".$pindex." = '".$pitem."' ";
                }
                $a = 1;
            }
        }
        if(!empty($order)){
            $sql .= $order;
        }
        $result = mysql_query($sql,$con);	//	查询数据库表
        $arr = array();
        while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
            $arr[] = $row;
        }
        mysql_close($con);
        return $arr;
    }
    //插入数据
    function insertData($table,$parm = array()){
        $con = myConnect::link_mysql();
        mysql_select_db(db,$con);

        $sql = " INSERT INTO ".$table;
        if(!empty($parm)){
            $field = "";
            $fieldContent = "";
            $num = 1;
            foreach($parm as $pindex => $pitem){
                $pitem = addslashes($pitem);
                if($num <count($parm)){
                    $field = $field."`".$pindex."`,";
                    $fieldContent = $fieldContent."'".$pitem."',";
                }else{
                    $field = $field."`".$pindex."`";
                    $fieldContent = $fieldContent."'".$pitem."'";
                }
                $num++;
            }
            if(!empty($field) && !empty($fieldContent)){
                $sql = $sql." (".$field.") VALUES (".$fieldContent.")";
            }
        }


        $result = mysql_query($sql);
        mysql_close($con);
        return $result;
    }

    //插入数据
    function updateData($table,$parm = array(),$where = array()){
        $con = myConnect::link_mysql();
        mysql_select_db(db,$con);

        $sql = " UPDATE ".$table." SET";
        if(!empty($parm)){
            foreach($parm as $pindex => $pitem){
                $pitem = addslashes($pitem);
                $sql .= " ".$pindex."='".$pitem."',";
            }
            $sql = substr($sql,0,-1);
        }
        if(!empty($where)){
            $sql .= " WHERE ";
            foreach($where as $windex => $witem){
                $sql .= " ".$windex." ='".$witem."' AND";
            }
        }
        $sql = substr($sql,0,-3);
        $result = mysql_query($sql);
        mysql_close($con);

        return $result;
    }
    function queryArr($sql){
        $con = myConnect::link_mysql();
        mysql_select_db(db,$con);
        $result = mysql_query($sql,$con);	//	查询数据库表
        $arr = array();
        while($row = mysql_fetch_array($result,MYSQL_ASSOC)){
            $arr[] = $row;
        }
        mysql_close($con);
        return $arr;
    }
    //执行sql语句
    function query($sql){
        $con = myConnect::link_mysql();
        mysql_select_db(db,$con);
        mysql_query($sql);
        mysql_close($con);
        return true;
    }

}
?>